﻿/*
 * LINQ to LDAP
 * http://linqtoldap.codeplex.com/
 * 
 * Copyright Alan Hatter (C) 2010-2012
 * alan.hatter@gmail.com - http://hattercoding.blogspot.com/
 * 
 * This project is subject to licensing restrictions. Visit http://linqtoldap.codeplex.com/license for more information.
 */

using System;
using System.Collections.Generic;

namespace LinqToLdap.Helpers
{
    /// <summary>
    /// Static class for parsing distinguished name information
    /// </summary>
    public static class DnParser
    {
        /// <summary>
        /// Parses the first name of an entry without the prefix (CN, OU, etc.) from <paramref name="distinguishedName"/> and returns that value.
        /// </summary>
        /// <param name="distinguishedName">The distinguished name to parse.</param>
        /// <returns></returns>
        public static string ParseName(string distinguishedName)
        {
            if (distinguishedName.IsNullOrEmpty())
            {
                throw new ArgumentException(string.Format("Invalid distinguished name '{0}'.", distinguishedName), "distinguishedName");
            }
            int startIndex = distinguishedName.IndexOf('=');
            if (startIndex <= 0)
            {
                throw new ArgumentException(
                    string.Format("Name could not be parsed from distinguished name '{0}'.", distinguishedName),
                    "distinguishedName");
            }
            startIndex += 1;

            int endIndex = distinguishedName.IndexOf(',');
            if (endIndex <= startIndex)
            {
                throw new ArgumentException(
                    string.Format("Name could not be parsed from distinguished name '{0}'.", distinguishedName),
                    "distinguishedName");
            }
            endIndex -= startIndex;

            return distinguishedName.Substring(startIndex, endIndex);
        }

        internal static string GetEntryName(string distinguishedName)
        {
            if (distinguishedName.IsNullOrEmpty())
            {
                throw new ArgumentException(string.Format("Invalid distinguished name '{0}'.", distinguishedName), "distinguishedName");
            }
            int index = distinguishedName.IndexOf(',');
            if (index <= 0)
            {
                throw new ArgumentException(
                    string.Format("Common name could not be parsed from distinguished name '{0}'.", distinguishedName),
                    "distinguishedName");
            }
            return distinguishedName.Substring(0, index);
        }

        internal static string GetEntryContainer(string distinguishedName)
        {
            if (distinguishedName.IsNullOrEmpty())
            {
                throw new ArgumentException(string.Format("Invalid distinguished name '{0}'.", distinguishedName), "distinguishedName");
            }
            int index = distinguishedName.IndexOf(',');
            if (index <= 0)
            {
                throw new ArgumentException(
                    string.Format("Common name could not be parsed from distinguished name '{0}'.", distinguishedName),
                    "distinguishedName");
            }
            return distinguishedName.Substring(index + 1, distinguishedName.Length - index - 1);
        }

        internal static string FormatName(string name, string currentDistinguishedName)
        {
            if (name.IndexOf("=") >= 0) return name;

            int index = currentDistinguishedName.IndexOf("=");

            return currentDistinguishedName.Substring(0, index + 1) + name;
        }
    }
}
